﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using phudieuhungthinh.BL;
using phudieuhungthinh.BL.Model;
using System.Web.Services;

namespace phudieuhungthinh.Admin
{
    public partial class CategoryList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod(EnableSession = true)]
        public static object GetCategoryList()
        {
            try
            {
                //Get data from database.
                var categoryImpl = new CategoryImpl();
                List<CategoryModel> listCategory = categoryImpl.GetCategoryModelList();
                //Return result to jtable.
                return new { Result = "OK", Records = listCategory, TotalRecordCount = listCategory.Count };
            }
            catch(Exception ex)
            {
                return new { Result = "Error", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static bool DeleteCategory(int categoryId)
        {
            try
            {
                //delete data from database.
                var categoryImpl = new CategoryImpl();
                var categoryModel = categoryImpl.GetCategoryModel(categoryId);
                categoryImpl.DeleteCategory(categoryModel);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        [WebMethod(EnableSession=true)]
        public static object GetProductCategories(int categoryId)
        {
            try
            {
                var productCategoriesImpl = new ProductCategoriesImpl();
                List<ProductCategoriesModel> productCategories = productCategoriesImpl.GetProductCategoriesByCategory(categoryId);
                if (productCategories.Count == 0)
                {
                    return null;
                }
                return productCategories;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}